package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] userIds, Integer[] menuIds){
        //调用service插入数据
        try {
            roleService.add(role,permissionIds,userIds,menuIds);
            return new Result(true,"角色添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色添加失败！");
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Role role, Integer[] permissionIds, Integer[] userIds, Integer[] menuIds){
        try {
            roleService.update(role,permissionIds,userIds,menuIds);
            return new Result(true,"修改角色信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改角色信息失败！");
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> lists = roleService.findAll();
            return new Result(true,"数据查询成功！",lists);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"数据查询失败！");
        }

    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true,"角色数据删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色数据删除失败！");
        }
    }

    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Role role=roleService.findById(id);
            return new Result(true,"角色信息查询成功！",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色信息查询失败！");
        }
    }
}
